﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using CVsharp.MVClib.Authorize;
using CVsharp.MVClib.ICO.Unity;

namespace CVsharp.DWZMvc
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // 路由名称
                "{controller}/{action}/{id}", // 带有参数的 URL
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值
            );
            routes.MapRoute("NoAction", "{controller}", new { controller = "home", action = "index", id = "" });//无Action的匹配 
            routes.MapRoute("NoID", "{controller}/{action}", new { controller = "home", action = "index", id = "" });//无ID的匹配 
            routes.MapRoute("Root", "", new { controller = "home", action = "index", id = "" });//根目录匹配   

        }

        //应用程序开始的时候初始的缓存
        private void initApplicationCache()
        {
            //所有被维护的Action权限
            ActionPermissionService aps = new ActionPermissionService();
            CVsharp.Models.DAL.Account.SysActionPermission sap = new CVsharp.Models.DAL.Account.SysActionPermission();
            int regCount = sap.Register(aps.GetAllActionByAssembly("CVsharp.DWZMvc"));
            Console.WriteLine(regCount);
        }


        private void initContainer()
        {
            ControllerBuilder.Current.SetControllerFactory(new UnityControllerFactory(ContainerFactory.GetContainer()));

        }
        protected void Application_Start()
        {
            #region
            initContainer();
            AreaRegistration.RegisterAllAreas();
            RegisterRoutes(RouteTable.Routes);
            initApplicationCache();
            #endregion
        }
    }
}